#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
@Time    : 2025/5/8 15:40
@Author  : HZP
@File    : auth_hander.py
"""
from dataclasses import dataclass

from flask_login import login_required, logout_user
from injector import inject

from internal.service import AccountService
from pkg.response import json_success,validate_error_json
from internal.schema.login_schema import PasswordLoginResp,LoginSchema


@inject
@dataclass
class AuthHandler:
    """LLMOps平台自有授权认证处理器"""
    account_service: AccountService

    def password_login(self):
        validate=LoginSchema()
        if not validate.validate():
            return validate_error_json(validate.errors)
        # 2.调用服务登录账号
        credential = self.account_service.password_login(validate.email.data, validate.password.data)

        # 3.创建响应结构并返回
        resp = PasswordLoginResp()

        """账号密码登录"""
        return json_success(resp.dump(credential))

    @login_required
    def logout(self):
        """退出登录，用于提示前端清除授权凭证"""
        logout_user()
        return json_success("退出登陆成功")
